using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NSRE.Math;


namespace Mosos
{

    public class Simulation
    {
        // Constante pour modifier la taille de la scene - prevoir plus large si beaucoup d'IA
        const int WORLDSIZE = 800;

        // A mettre a false si on ne veut pas de joueur
        const bool PLAYER_AVAILABLE = true;

        const int max_size = 32;
        private List<Cell> cells;
        private List<AI_interface> interfaces;
        private Cell player;
        MGraphics.World world;
        
        float time_speed = 1.0f;
    


        public Simulation()
        {
            world = new MGraphics.World(WORLDSIZE, WORLDSIZE, "MOSOS");
        }
        // Cest ICI que vous ajoutez vos IA a la scene
        private void Load_AI()
        {
            interfaces = new List<AI_interface>();
            // ICI on ajoute 1 IA de type AI_roux_b
            for (int i = 0; i < 1; i++)
                interfaces.Add(new AI_roux_b());
            for (int i = 0; i < 1; i++)
                interfaces.Add(new AI_salaun_n("AIsalaun_n.png"));
        }

        public void Launch()
        {

            Load_AI();
            cells = WorldGenerator.generate_world(WORLDSIZE,
                                                  interfaces.ToArray(),
                                                  32,
                                                  world);
            world.NSREWindow.On_Draw.Add(this.UpdateScene);
            if (PLAYER_AVAILABLE)
            {
                player = new Cell(new Vector2(400, 100), max_size - 5, new AI_player(world), "player", world);
                cells.Add(player);
                world.Turn_Red_Cell("player");
                world.NSREWindow.On_Click.Add(this.HandleMouse);
            }
            world.NSREWindow.Keyboard.On_Key_Down(NSRE.Events.Keyboard.Key.Up, this.increase_time_speed);
            world.NSREWindow.Keyboard.On_Key_Down(NSRE.Events.Keyboard.Key.Down, this.decrease_time_speed);
            world.Run();
        }

        public void increase_time_speed()
        {
            if (time_speed < 4)
                time_speed += 0.1f;
            else
                time_speed = 4;
            string ts = time_speed.ToString();
            int max = ts.Length;
            if (max > 3)
                max = 3;
            Console.WriteLine("Time speed = " + time_speed.ToString().Substring(0, max));
        }
        public void decrease_time_speed()
        {
            if (time_speed > 0)
                time_speed -= 0.1f;
            else
                time_speed = 0;
            string ts = time_speed.ToString();
            int max = ts.Length;
            if (max > 3)
                max = 3;
            Console.WriteLine("Time speed = " + time_speed.ToString().Substring(0, max));
        }
        protected void UpdateScene()
        {
            Compute_Collisions();
            Update_Cells((float)world.NSREWindow.FPS * time_speed);
        }

        public void HandleMouse()
        {
            player.call_AI(cells.ToArray());
        }

        protected void Compute_Collisions()
        {
            Cell[] cell_array = cells.ToArray();
            for (int i = 0; i < cells.Count; i++)
                for (int j = i + 1; j < cells.Count; j++)
                {
                    cell_array[i].collision_cell(cell_array[j]);
                    if (!cell_array[i].is_alive())
                        cells.Remove(cell_array[i]);
                    else if (!cell_array[j].is_alive())
                        cells.Remove(cell_array[j]);
                }
        }

        protected void Update_Cells(float elapsed)
        {
            Cell[] cell_array = cells.ToArray();
            foreach (Cell cell in cells)
            {
                cell.collision_wall(WORLDSIZE, WORLDSIZE);
                cell.update(elapsed);

                if (cell != player)
                    cell.call_AI(cell_array);
            }
            
        }

     
    }
}
